﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
using Eb.MessageBox;
public partial class Modules_UserManager_ucSearchUserGroup : System.Web.UI.UserControl
{
    UserManager _userGroupManager = new UserManager();
    protected void Page_Load(object sender, EventArgs e)
    {
       // btnDelete.Attributes.Add("OnClick", "return confirmDelete()");
        if (!IsPostBack)
        {
            Session["UserManager_UserGroupSearch"] = "Latest";
            Search();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Text == string.Empty)
            Session["UserManager_UserGroupSearch"] = "BlankSearch";
        else
            Session["UserManager_UserGroupSearch"] = txtSearch.Text;

        Search();   
    }

    void Search()
    {
        try
        {
            DataSet ds = new DataSet();
            _userGroupManager.SearchUserGroup(Session["UserManager_UserGroupSearch"].ToString(), out ds);

            gvwUserGroup.DataSource = ds;
            gvwUserGroup.Columns[1].Visible = true;
            gvwUserGroup.DataBind();
            gvwUserGroup.Columns[1].Visible = false;

            if (ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("No records found");
               // Response.Write("<script>alert('No Records Found !')</script>");
            }

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search UserGroup Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Delete();
    }

    void Delete()
    {       
        int index = 0;

        string[] userGroupIDs = new string[gvwUserGroup.Rows.Count];
        for (int i = 0; i < gvwUserGroup.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwUserGroup.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                userGroupIDs[index] = gvwUserGroup.Rows[i].Cells[1].Text;
                index++;
            }
        }

        if (index == 0)
        {
            MessageBox.Show("Select utility");
          //  Response.Write("<script>alert('Select Utility Group')</script>");
            return;
        }

        string conditionDeleteUserGroupMap = "UR_UserGroupID='" + userGroupIDs[0] + "'";
        string conditionDeleteUserGroup = "US_GroupID='" + userGroupIDs[0] + "'";
        for (int i = 1; i < index; i++)
        {
            conditionDeleteUserGroupMap = conditionDeleteUserGroupMap + "or UR_UserGroupID='" + userGroupIDs[i] + "'";
            conditionDeleteUserGroup = conditionDeleteUserGroup + "or US_GroupID='" + userGroupIDs[i] + "'";
        }
        _userGroupManager.DeleteUserGroup(conditionDeleteUserGroupMap, conditionDeleteUserGroup);
        MessageBox.Show("Successfully Deleted");
     //   Response.Write("<script>alert('Deleted Successfully !')</script>");
        Search();
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {
            Session["UserManager_UserGroupEdit"] = "Edit";
            Session["UserManager_Control"] = "/UserManager/ucAddUserGroup.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx");
        }
    }

    bool CheckSelection()
    {
        int count = 0, selectedRow = -1;

        for (int i = 0; i < gvwUserGroup.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwUserGroup.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                count++;
                selectedRow = i;
            }
        }

        if (count == 0)
        {
            MessageBox.Show("Select user Group");
           // Response.Write("<script>alert('Select User Group')</script>");
            return false;
        }
        else if (count > 1)
        {
            MessageBox.Show("Select single user group");
           // Response.Write("<script>alert('Select Single User Group')</script>");
            return false;
        }

        Session["UserManager_UserGroupID"] = gvwUserGroup.Rows[selectedRow].Cells[1].Text;
        return true;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session["UserManager_UserGroupEdit"] = "Add";
        Session["UserManager_Control"] = "UserManager/ucAddUserGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnUsers_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {
            Session["UserManager_Control"] = "UserManager/ucUserGroupMap.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx");
        }
    }
    protected void gvwUserGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
}
